package com.javandad.app.server.mail.entity;

import java.io.UnsupportedEncodingException;


import java.util.*;

import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;


import com.javandad.app.shared.entity.Mailable;
import com.javandad.app.shared.entity.User;


public class WelcomeMail extends JMail {

	public WelcomeMail(User user) {
		super(Session.getDefaultInstance(new Properties(), null));
		adapt(user);
	}
	
	protected void adapt(Mailable a){
		User user = (User) a;
		try {
			this.setSubject("Welcome to JaVandad!");
			String content = getContent(user);
			this.setDataHandler(new DataHandler( new JMail.HTMLDataSource(content))) ;
			
			this.setFrom(new InternetAddress("javandad.info@gmail.com",
					"JaVandad"));
			this.addRecipient(RecipientType.TO, new InternetAddress(user.getEmail()));
		} catch (MessagingException e) {
			e.printStackTrace();
		}catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		
	}
	
	private String getContent(User user){
		String c = "";
		
		c = "<table><tr><td>" +
				"Dear " + user.getFirstName() != null ? user.getFirstName() + "!": "" +
				"</td></tr>" +
				"<tr><td>" +
				"Thanks for joining us and we wish you best of luck with your projects. " +
				"</td></tr><tr><td>" +
				"With Best Regards<br>JaVandad Group." +
				"</td></tr>" + 
				"</table>";
			
		return c;
	}
	
}
